package com.celesea.auth.position.v1.controller;

import com.celesea.auth.dept.service.DeptService;
import com.celesea.auth.position.entity.Position;
import com.celesea.auth.position.service.PositionService;
import com.celesea.auth.position.v1.controller.request.ConfigRequest;
import com.celesea.auth.position.v1.controller.request.PageRequest;
import com.celesea.auth.position.v1.controller.request.PostRequest;
import com.celesea.auth.position.v1.controller.request.PutRequest;
import com.celesea.auth.position.v1.controller.resource.PageResource;
import com.celesea.auth.position.v1.controller.resource.PositionResource;
import com.celesea.auth.position.vo.PositionFilter;
import com.celesea.auth.position.vo.PositionVo;
import com.celesea.auth.role.entity.RoleCode;
import com.celesea.framework.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author JackYuan
 * @create 2017-10-28 21:17
 */
@RestController
@RequestMapping("v1/positions")
@Api(tags = "岗位接口")
@PreAuthorize("hasAnyAuthority('" + RoleCode.ADMIN_SYSTEM + "','" + RoleCode.ADMIN_BUS + "')")
public class PositionController {

    @Autowired
    private PositionService positionService;
    @Autowired
    private DeptService deptService;

    @GetMapping
    @ApiOperation(value = "岗位分页")
    public PageData<PageResource> page(@Valid PageRequest request, Page page, Sort sort) {
        //TODO 父部门Id，子部门Id
        PositionFilter filter = new PositionFilter(request);
        if (StringKit.isEmpty(filter.getDeptId())) {
            // root query
            filter.setDeptId(deptService.selectRootId(EnvUtil.getTenantId()));
        } else {
            filter.setParentIds(deptService.selectParents(request.getDeptId()));
        }
        PageData<Position> pageData = positionService.selectPage(filter, page, sort);
        return pageData.convert(item -> new PageResource(item));
    }

    @PostMapping
    @ApiOperation(value = "保存岗位")
    public PositionResource post(@RequestBody @Valid PostRequest request) {
        PositionVo entity = positionService.insert(new PositionVo(request));
        return new PositionResource(entity);
    }

    @PutMapping
    @ApiOperation(value = "更新岗位")
    public PositionResource put(@RequestBody @Valid PutRequest request) {
        PositionVo entity = positionService.update(new PositionVo(request));
        return new PositionResource(entity);
    }

    @GetMapping(value = "/{id}")
    @ApiOperation(value = "根据id查询岗位")
    public PositionResource get(@PathVariable String id, @ApiParam("在某个部门下查询") String deptId) {
        if (StringKit.isEmpty(deptId)) {
            deptId = deptService.selectRootId(EnvUtil.getTenantId());
        }
        PositionVo position = positionService.selectById(id, deptId);
        return new PositionResource(position);
    }

    @GetMapping(value = "/{deptId}/child")
    @ApiOperation(value = "根据id查询岗位")
    public PositionResource child(@PathVariable String deptId) {
        PositionVo position = positionService.buildByDept(deptId);
        return new PositionResource(position);
    }

    @DeleteMapping(value = "/{id}")
    @ApiOperation(value = "根据id删除岗位")
    public void delete(@PathVariable String id) {
        positionService.doDelete(id);
    }


    @GetMapping(value = "/{id}/config")
    @ApiOperation(value = "根据id查询岗位成员信息")
    public PositionResource getConfig(@PathVariable String id, @ApiParam("在某个部门下查询") String deptId) {
        PositionVo position = positionService.selectConfig(id, deptId);
        return new PositionResource(position);
    }

    @PutMapping(value = "/{id}/config")
    @ApiOperation(value = "根据id查询岗位成员信息")
    public PositionResource putConfig(@RequestBody @Valid ConfigRequest request) {
        PositionVo entity = positionService.updateConfig(new PositionVo(request));
        return new PositionResource(entity);
    }

}
